﻿using System;
using System.Windows;

namespace SingleInstanceApplication.WpfApp
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow
	{
		/// <summary>
		/// Gets Previouse Window State
		/// </summary>
		public WindowState PreviouseWindowState { get; private set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="MainWindow"/> class.
		/// </summary>
		public MainWindow()
		{
			InitializeComponent();

			// store 1st value
			PreviouseWindowState = WindowState;

			// attach to event (used to store prev. win. state)
			LayoutUpdated += Window_LayoutUpdated;
		}

		/// <summary>
		/// Occures on layout change
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Window_LayoutUpdated(object sender, EventArgs e)
		{
			PreviouseWindowState = WindowState;
		}

		/// <summary>
		/// Activates Window <para/>
		/// Example for this: http://blogs.microsoft.co.il/blogs/maxim/archive/2009/12/24/daily-tip-how-to-activate-minimized-window-form.aspx
		/// </summary>
		/// <param name="restoreIfMinimized">if [true] restore prev. win. state</param>
		/// <returns></returns>
		public bool Activate(bool restoreIfMinimized)
		{
			if (restoreIfMinimized && WindowState == WindowState.Minimized)
			{
				WindowState = PreviouseWindowState == WindowState.Normal
										? WindowState.Normal : WindowState.Maximized;
			}
			return Activate();
		}

		/// <summary>
		/// Apends the args.
		/// </summary>
		/// <param name="args">The args.</param>
		public void ApendArgs(string[] args)
		{
			if (args == null) return;
			txtArgs.AppendText(string.Format("[{0}] Received Args:\r\n", System.DateTime.Now));
			txtArgs.AppendText(string.Join("\r\n", args).Trim());
			txtArgs.AppendText("\r\n===============================");
		}
	}
}
